#！/bin/bash

source /etc/profile

if [ -n "$1" ]
then
	do_date=$1
else
	do_date=`date -d "-1 day" +%F`
fi
sql="
insert overwrite table dim.dim_trade_shops
select shopid,
        userid,
        areaid,
        shopname,
        shoplevel,
        status,
        createtime,
        modifytime,
        case when modifyTime is not null
          then substr(modifyTime,0,10)
          else substr(createTime,0,10)
        end as start_dt,
        '9999-12-31' as end_dt
from ods.ods_trade_shops
where dt='$do_date'

union all

select  dim.shopid,
        dim.userid,
        dim.areaid,
        dim.shopname,
        dim.shoplevel,
        dim.status,
        dim.createtime,
        dim.modifytime,
        dim.start_dt,
        case
        	when dim.end_dt >= '9999-12-31' and ods.shopid is not null
          then '$do_date'
          else dim.end_dt
        end as end_dt
from dim.dim_trade_shops dim
left join
(select *
    from ods.ods_trade_shops
    where dt='$do_date' ) ods
on dim.shopid = ods.shopid;
"
hive -e "$sql"